﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using Telerik.Web.UI;

namespace KPIS.GERP.GBMS.BLL
{
    public class Util
    {
        #region Public Method
        public Nullable<int> ConvertNullInt(Object o)
        {
            Nullable<int> result = o == null
                                       ? null
                                       : (o.ToString().Length == 0
                                              ? null
                                              : (Nullable<int>)Convert.ToInt32(o));
            return result;
        }

        public Nullable<double> ConvertNullDouble(Object o)
        {
            Nullable<double> result = o == null
                                          ? null
                                          : (o.ToString().Length == 0
                                                 ? null
                                                 : (Nullable<double>)Convert.ToDouble(o));
            return result;
        }

        public Nullable<DateTime> ConvertNullDateTime(Object o)
        {
            Nullable<DateTime> result = o == null
                                            ? null
                                            : (o.ToString().Length == 0
                                                   ? null
                                                   : (Nullable<DateTime>)Convert.ToDateTime(o)
                                              );
            return result;
        }

        public Nullable<TimeSpan> ConvertNullTimeSpan(Object o)
        {
            Nullable<TimeSpan> result = o == null
                                            ? null
                                            : (o.ToString().Length == 0
                                                   ? null
                                                   : (Nullable<TimeSpan>)Convert.ToDateTime(o).TimeOfDay);
            return result;
        }

        public string ConvertDBDateTimeToString(Object o, bool bLongDateString)
        {
            string result;

            if (bLongDateString)
                result = o == DBNull.Value ? "" : Convert.ToDateTime(o).ToLongDateString();
            else
                result = o == DBNull.Value ? "" : Convert.ToDateTime(o).ToShortDateString();

            return result;
        }

        public string MakeZero(object str)
        {
            double temp;
            return !Double.TryParse(Convert.ToString(str), out temp) ? "0" : Convert.ToString(str);
        }

        public int AlwayInt(string str)
        {
            int temp;
            return !Int32.TryParse(Convert.ToString(str), out temp) ? 0 : Convert.ToInt32(str);
        }

        public Double AlwayDouble(string str)
        {
            double temp;
            return !Double.TryParse(Convert.ToString(str), out temp) ? 0 : Convert.ToDouble(str);
        }

        public string WebString(string strValue)
        {
            string strResult = strValue.Replace("&nbsp;", "").Replace("\r\n", "<br />");

            return strResult;
        }

        public string EncodeHtml(string strValue = "")
        {
            string strResult = HttpUtility.HtmlEncode(strValue.Replace("'", "").Replace("\"", ""));

            return strResult;
        }

        public string DBString(string strValue)
        {
            strValue = strValue.Replace("–", "-");

            return (strValue.Length == 0 ? null : strValue);
        }

        public string LabelToTextbox(string strValue)
        {
            string strResult = strValue.Replace("<br />", "\r\n");

            return strResult;
        }

        public void MessageAlert(Page p, string strMessage)
        {
            ScriptManager.RegisterClientScriptBlock(p, p.GetType(), "onclick"
                , "window.alert('" + strMessage + "');", true);
        }

        public void OpenPopup(Page p, string strPath, string strPopupName, string[] value)
        {
            string strScript = "radopen('" + strPath + "?";

            if (value != null)
            {
                for (int i = 0; i < value.Length; i++)
                {
                    strScript += string.Format("param{0}={1}&", i + 1, value[i]);
                }
            }

            strScript += "', '" + strPopupName + "')";

            ScriptManager.RegisterClientScriptBlock(p, p.GetType(), "onclick"
                , strScript, true);
        }

        public void InsertEmptyDDLSelect(RadComboBox ddl)
        {
            RadComboBoxItem empty = new RadComboBoxItem("<เลือกรายการ>", "");
            ddl.Items.Insert(0, empty);
        }

        public void InsertEmptyDDLAll(RadComboBox ddl)
        {
            RadComboBoxItem empty = new RadComboBoxItem("<ทั้งหมด>", "");
            ddl.Items.Insert(0, empty);
        }

        public RadMenuItem FindRadMenuItemRecursive(RadMenu radMenu, string searchText
            , bool exactlyWord = false)
        {
            return radMenu.GetAllItems().FirstOrDefault(radMenuItem => radMenuItem.Text.Contains(searchText));
        }

        /// <summary>
        /// If parameter str is Integer, Then return true
        /// If It empty string or space or charactor or contain number not in format int, return false
        /// </summary>
        /// <param name="str">string to check is it integer</param>
        /// <returns></returns>
        public bool IsInt32(string str)
        {
            if (String.IsNullOrEmpty(str))
                return false;

            str = str.Trim();

            int r;
            return Int32.TryParse(str, out r);
        }

        /// <summary>
        /// If parameter str is Integer or Double, Then return true
        /// If It empty string or space or charactor or contain number not in format double or int, return false
        /// </summary>
        /// <param name="str">string to check is it double or integer</param>
        /// <returns></returns>
        public bool IsDouble(string str)
        {
            if (String.IsNullOrEmpty(str))
                return false;

            str = str.Trim();

            double r;
            return Double.TryParse(str, out r);
        }

        public Control FindControlRecursive(Control container, string name)
        {
            if (container.ID == name)
                return container;

            foreach (Control ctrl in container.Controls)
            {
                Control foundCtrl = FindControlRecursive(ctrl, name);
                if (foundCtrl != null)
                    return foundCtrl;
            }
            return null;
        }
        #endregion

        #region Properties
        public enum EditMode
        {
            AddNew
            ,
            Edit
                , View
        }
        #endregion
    }
}
